package main

import (
	"fmt"
)

type HashTable struct {
	LinkArr [7]EmpLink
}
type EmpLink struct {
	head *Emp
}

type Emp struct {
	Id   int
	Name string
	Next *Emp
}

func (this Emp) Show() {
	fmt.Printf("雇员%d 在%d链表中", this.Id, this.Id%7)
}
func (this *EmpLink) Show(id int) {
	if this.head == nil {
		fmt.Printf("连表%d 雇员为空\n", id)
		return
	}
	cur := this.head
	for {
		if cur != nil {
			fmt.Printf("链表%d 雇员[id=%d name=%s] ->", id, cur.Id, cur.Name)
			cur = cur.Next
		} else {
			break
		}
	}
	fmt.Println()
}
func (this *EmpLink) findByid(id int) *Emp {
	cur := this.head
	for {
		if cur != nil && cur.Id == id {
			return cur
		} else if cur == nil {
			break
		}
		cur = cur.Next
	}
	return nil
}
func (this *EmpLink) Insert(emp *Emp) {
	cur := this.head
	var pre *Emp = nil
	if cur == nil {
		this.head = emp
		return
	}
	for {
		if cur.Next != nil {
			if cur.Id >= emp.Id {
				break
			}
			pre = cur
			cur = cur.Next
		} else {
			break
		}
	}
	pre.Next = emp
	emp.Next = cur
}

func (this *HashTable) ShowAll() {
	for i := 0; i < len(this.LinkArr); i++ {
		this.LinkArr[i].Show(i)
	}
}

func (this *HashTable) Insert(emp *Emp) {
	linkNo := this.HashNum(emp.Id)
	this.LinkArr[linkNo].Insert(emp)
}

func (this *HashTable) findByid(id int) *Emp {
	linkNo := this.HashNum(id)
	return this.LinkArr[linkNo].findByid(id)
}
func (this *HashTable) HashNum(id int) int {
	return id % 7
}

func EmpHand() {
	h1 := HashTable{}
	var key = ""
	for {
		fmt.Println("雇员的菜单")
		fmt.Println("input 表示添加会员")
		fmt.Println("show 表示添加会员")
		fmt.Println("find 表示添加会员")
		fmt.Println("exit 表示添加会员")
		fmt.Print("请输入你的选择:")
		fmt.Scanln(&key)
		switch key {
		case "input":
			fmt.Print("请输入雇员ID:")
			var id int
			fmt.Scanln(&id)
			fmt.Print("请输入雇员名称:")
			var name string
			fmt.Scanln(&name)
			emp := Emp{
				Id:   id,
				Name: name,
			}
			h1.Insert(&emp)
		case "find":
			fmt.Print("请输入雇员ID:")
			var id int
			fmt.Scanln(&id)
			emp := h1.findByid(id)
			if emp == nil {
				fmt.Println("未找到雇员:id=", id)
			} else {
				emp.Show()
			}
		case "show":
			h1.ShowAll()
		case "exit":
		default:
			fmt.Println("输入有误")
		}
	}
}
